import allure,time
from selenium.webdriver.common.by import By
from page.page_common_steps import PageCommonSteps
from base.base_common import BaseCommon

class PageBaseDataMgt(PageCommonSteps):
    def __init__(self, driver):
        super().__init__(driver) # 继承公用方法
        # 元素：菜单栏/系统管理
        self.menu_system_mgt = By.XPATH, "//body/div[@id='app']/div[1]/div[1]/div[1]/div[2]/div[2]/ul[1]/li[4]"
        # 元素：菜单栏/基础数据管理
        self.menu_base_data_mgt = By.XPATH, "//body/div[@id='app']/div[1]/div[2]/ul[1]/li[2]"


        ####产品管理
        # 元素：产品管理/搜索
        self.search_by_product_name = By.XPATH, "//*[@placeholder='产品名称']"
        # 元素：产品管理/搜索
        self.search_by_product_level = By.XPATH, "//*[@placeholder='层次']"
        # 元素：产品管理/搜索
        self.search_by_product_major = By.XPATH, "//*[@placeholder='专业']"
        # 元素：产品管理/搜索
        self.search_by_product_status = By.XPATH, "//*[@placeholder='状态']"
        # 元素：产品管理/搜索
        self.search_by_product_org = By.XPATH, "//*[@placeholder='所属机构']"
        # 元素：产品管理/添加产品
        self.btn_add_product = By.XPATH, "//span[contains(text(),'添加产品')]"
        # 元素：产品管理/批量设置授权
        self.btn_product_to_empower = By.XPATH, "//span[contains(text(),'批量设置授权')]"
        # 元素：产品管理/添加产品/层次
        self.input_product_level = By.XPATH, "//label[contains(text(),'层次:')]/following-sibling::div/div/div/div/input"
        # 元素：产品管理/添加产品/专业
        self.input_product_major = By.XPATH, "//label[contains(text(),'专业:')]/following-sibling::div/div/div/div/div/div/input"
        # 元素：产品管理/添加产品/服务周期
        self.input_product_service_period = By.XPATH, "//label[contains(text(),'服务周期:')]/following-sibling::div/div/input"
        # 元素：产品管理/添加产品/标准收费
        self.input_product_standard = By.XPATH, "//label[contains(text(),'标准收费:')]/following-sibling::div/div/input"
        # 元素：产品管理/添加产品/最低价格
        self.input_product_minimum = By.XPATH, "//label[contains(text(),'最低价格:')]/following-sibling::div/div/input"
        # 元素：产品管理/设置授权
        self.btn_data_to_empower = By.XPATH, "//tbody/tr[1]/td[1]/div[1]/div[1]/div[1]/*[contains(text(),'设置授权')]"
        # 元素：产品管理/设置授权/全选勾选
        self.select_all_authorization = By.XPATH,"//label[contains(text(),'授权教学中心:')]/following-sibling::div/label/span"

        ####专业管理
        # 元素：专业管理/搜索
        self.search_by_major_name = By.XPATH, "//*[@placeholder='专业名称']"
        # 元素：专业管理/搜索
        self.search_by_major_level = By.XPATH, "//*[@placeholder='层次']"
        # 元素：专业管理/搜索
        self.search_by_major_status = By.XPATH, "//*[@placeholder='状态']"
        # 元素：专业管理/搜索
        self.search_by_major_org = By.XPATH, "//*[@placeholder='所属机构']"
        # 元素：专业管理/添加专业
        self.btn_add_major = By.XPATH, "//span[contains(text(),'添加专业')]"
        # 元素：专业管理/添加专业/层次
        self.add_major_input_level = By.XPATH, "//label[contains(text(),'层次:')]/following-sibling::div/div/div/div/span"
        # 元素：专业管理/添加专业/专业名称
        self.add_major_input_name = By.XPATH, "//label[contains(text(),'专业名称:')]/following-sibling::div/div/input"
        # 元素：专业管理/添加专业/专业编码
        self.add_major_input_code = By.XPATH, "//label[contains(text(),'专业编码:')]/following-sibling::div/div/input"
        # 元素：专业管理/添加专业/备注
        self.add_major_input_tips = By.XPATH, "//label[contains(text(),'备注:')]/following-sibling::div/div/textarea"

    @allure.step(title="操作步骤:点击菜单栏/基础数据管理/子菜单项")
    def click_menu_base_data_mgt(self,choose=1):
        time.sleep(2)
        self.click(self.menu_system_mgt) # 点击菜单栏/系统管理
        time.sleep(1)
        self.click(self.menu_base_data_mgt)# 点击菜单栏/基础数据管理
        time.sleep(1)
        menu_choose = By.XPATH, "//body/div[@id='app']/div[1]/div[2]/ul[1]/li[2]/ul[1]/li[{0}]".format(choose)
        self.click(menu_choose)
        return time.sleep(2)

    @allure.step(title="操作步骤:创建产品")
    def click_to_add_product(self,level,major,service_period,status,standard,minimum):
        self.click(self.btn_add_product) #点击新建按钮
        self.click_and_input_keys_to_select(locate=self.input_product_level, keys=level) #选择层次
        self.click_and_input_keys_to_select(locate=self.input_product_major, keys=major) #选择专业
        self.click_and_input_keys(locate=self.input_product_service_period,keys=service_period) #设置服务周期
        self.click_give_text_radio(text=status)  # 设置产品状态
        self.click_and_input_keys(locate=self.input_product_standard, keys=standard)  # 设置标准收费
        self.click_and_input_keys(locate=self.input_product_minimum, keys=minimum)  # 设置最低价格
        self.click_to_save()#点击保存

    @allure.step(title="操作步骤:编辑产品")
    def click_to_edit_product(self,service_period,status,standard,minimum):
        self.click_to_data_edit() #点击编辑按钮
        self.clear(feature=self.input_product_service_period)
        self.click_and_input_keys(locate=self.input_product_service_period,keys=service_period) #设置服务周期
        self.click_give_text_radio(text=status)  # 设置产品状态
        self.click_and_input_keys(locate=self.input_product_standard, keys=standard)  # 设置标准收费
        self.click_and_input_keys(locate=self.input_product_minimum, keys=minimum)  # 设置最低价格
        self.click_to_save()#点击保存
        if status == "禁用": self.alert_click_to_confirm()  # 关闭二次确认弹窗

    @allure.step(title="操作步骤:创建专业")
    def click_to_add_major(self,level,major,code,status,tips):
        self.click(self.btn_add_major) #点击新建按钮
        self.click_and_input_keys_to_select_3(locate=self.add_major_input_level,order=1, keys=level) #选择层次
        self.click_and_input_keys(locate=self.add_major_input_name, keys=major) #输入专业
        self.click_and_input_keys(locate=self.add_major_input_code,keys=code) #输入专业编码
        self.click_give_text_radio(text=status)  # 设置产品状态
        self.click_and_input_keys(locate=self.add_major_input_tips, keys=tips)  # 输入备注
        self.click_to_confirm()#点击确认

    @allure.step(title="操作步骤:编辑产品")
    def click_to_edit_major(self,major,code,status,tips):
        self.click_to_data_edit() #点击编辑按钮
        self.click_and_input_keys(locate=self.add_major_input_name, keys=major) #输入专业
        self.click_and_input_keys(locate=self.add_major_input_code,keys=code) #输入专业编码
        self.click_give_text_radio(text=status)  # 设置产品状态
        self.click_and_input_keys(locate=self.add_major_input_tips, keys=tips)  # 输入备注
        self.click_to_confirm()#点击确认
        self.click((By.XPATH, "//p[@style='margin: 20px 30px;']/parent::div/following-sibling::div/div/button[2]")) #修改专业确认
        if status == "禁用":
            self.click((By.XPATH,"//p[text()='状态设为禁用后创建产品和培养方案时将不能使用该专业，确定设置为禁用吗？']/parent::div/following-sibling::div/div/button[2]"))
            # self.find_els(feature=self.confirm_btn)[0].click()# 关闭二次确认弹窗

